/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.babeuf.form.fields.input
{
	import flash.events.FocusEvent;
	import flash.text.TextField;
	import net.babeuf.form.fields.IField;
	/**
	 * ...
	 * @author damien pegoraro
	 */
	public class NumericField extends InputField implements IField
	{
		protected var _minValue : Number = Number.MIN_VALUE; 
		protected var _maxValue : Number = Number.MAX_VALUE ; 
		
		
		
		public function NumericField( textField : TextField, min : uint = 0, max : uint =  255) 
		{			
			super(textField, min, max) ;
			_textField.restrict = "0123456789"; 
		}
		
		override protected function _handleFocusIn(e:FocusEvent):void 
		{			
			super._handleFocusIn(e) ; 
			_textField.restrict = "0123456789"; 
			
		}
		
		override protected function _handleFocusOut(e:FocusEvent):void 
		{			
			super._handleFocusOut(e) ; 
			_textField.restrict = null; 
	
		}
		
		public function setLimits( min : Number = NaN, max : Number = NaN ) : void
		{
			if ( ! isNaN( min )) _minValue = min ; 
			if ( ! isNaN( max )) _maxValue = max ; 
		}
		
		
		
		override public function validate():Boolean 
		{
			if ( !super.validate() ) return false ; 	
			if ( optional && getValue() == "" ) return true ; 			
			if ( Number(getValue()) < _minValue || Number(getValue()) > _maxValue)  _error = _invalidError ;   			
			if ( _error != "" ) {_updateSensors(false) ; return false ; }			
			_updateSensors(true) ; 
			return true ; 
		}
		
		override public function empty():void 
		{
			super.empty();
			_textField.restrict = null; 
		}
		
	}

}